package com.example.leo.mycharmingstudent.project.maintabfragment.subpagefragment.adapter;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.leo.mycharmingstudent.R;
import com.example.leo.mycharmingstudent.project.entity.KnowledgeTreeEntity;

import java.util.List;

/**
 * Created by Leo on 2020/9/30.
 */

public class SubPageIndicatorAdapter
        extends RecyclerView.Adapter<SubPageIndicatorAdapter.IndicatorHolder> {
    //指示器recyclerview
    private RecyclerView rvIndicator;
    //指示器条目数据列表
    private List<KnowledgeTreeEntity.DataBean.ChildrenBean> mChildrenBeanList;

    private int mSelectPosition = 0;

    public SubPageIndicatorAdapter(
            RecyclerView rvIndicator,
            List<KnowledgeTreeEntity.DataBean.ChildrenBean> childrenBeanList) {
        this.rvIndicator = rvIndicator;
        this.mChildrenBeanList = childrenBeanList;
    }

    /**
     * 创建视图View
     * @param parent
     * @param viewType
     * @return
     */
    @Override
    public IndicatorHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_sub_page_indicator_adapter, parent, false);
        return new IndicatorHolder(inflate);
    }

    /**
     * 设置数据
     * @param holder
     * @param position
     */
    @Override
    public void onBindViewHolder(final IndicatorHolder holder, int position) {
        if (mSelectPosition == position) {
            //表示当前position是选中的位置
            holder.tvIndicator.setSelected(true);
        }else {
            //表示当前position不是选中的位置
            holder.tvIndicator.setSelected(false);
        }
        final KnowledgeTreeEntity.DataBean.ChildrenBean childrenBean = mChildrenBeanList.get(position);
        holder.tvIndicator.setText(childrenBean.getName());

        if (mListener != null) {
            holder.tvIndicator.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int adapterPosition = holder.getAdapterPosition();
                    mListener.onIndicatorItemClick(
                            adapterPosition,
                            childrenBean);
                    //了解即可，不需掌握
//                    scrollToCenter(holder.getAdapterPosition());

                    holder.tvIndicator.setSelected(true);

                    mSelectPosition = adapterPosition;
                }
            });
        }
    }

    public void setCurrentItem(int position){
        mSelectPosition = position;
        /*
         * 告诉recyclerview的adapter（SubPageIndicatorAdapter），
         * 所有数据发生变化，需要更新数据显示
         * onBindViewHolder方法会执行
         */
        notifyDataSetChanged();
    }

    /**
     * 滚动指示器recyclerview，
     * 使选中条目居中
     * 了解即可，不需掌握
     */
    /*private void scrollToCenter(int position) {
        LinearLayoutManager layoutManager = (LinearLayoutManager) rvIndicator.getLayoutManager();
        int firstPosition = layoutManager.findFirstVisibleItemPosition();
        int lastPosition = layoutManager.findLastVisibleItemPosition();

        int left1 = rvIndicator.getChildAt(position - firstPosition).getLeft();
        int left2 = rvIndicator.getChildAt(lastPosition - position).getLeft();

        rvIndicator.scrollBy((left1 - left2) / 2, 0);

    }*/

    @Override
    public int getItemCount() {
        return mChildrenBeanList.size();
    }

    /**
     * 找控件
     */
    static class IndicatorHolder extends RecyclerView.ViewHolder {

        private TextView tvIndicator;

        public IndicatorHolder(View itemView) {
            super(itemView);
            tvIndicator = itemView.findViewById(R.id.tv_indicator_item_sub_page_indicator_adapter);
        }
    }

    private onIndicatorItemClickListener mListener;

    public void setOnIndicatorItemClickListener(onIndicatorItemClickListener listener) {
        this.mListener = listener;
    }

    public interface onIndicatorItemClickListener {

        void onIndicatorItemClick(
                int position,
                KnowledgeTreeEntity.DataBean.ChildrenBean childrenBean);
    }

}
